package com.hcms.admin.controller.sys;

import cn.hutool.crypto.SecureUtil;
import com.hcms.admin.security.AdminUserDetails;
import com.hcms.admin.service.ISysResourceService;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.SysAdmin;
import com.hcms.mybatis.entity.SysResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * <p>
 * 对象存储记录 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-02-23
 */
@RestController
@RequestMapping("/sysResource")
public class SysResourceController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISysResourceService sysResourceService;

    @PostMapping("/upload")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysResource>> upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        logger.info("Run upload(), originalFilename={}", originalFilename);
        if (originalFilename != null) {
            String sha1 = SecureUtil.sha1(file.getInputStream());
            logger.info("Upload file sha1: {}", sha1);
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Object principal = authentication.getPrincipal();
            if (principal instanceof AdminUserDetails) {
                AdminUserDetails userDetails = (AdminUserDetails) principal;
                Long adminId = userDetails.getSysAdmin().getId();
                SysResource storage = sysResourceService.uploadObject(file, adminId);
                logger.info("Saved resource successfully");
                return CommonResponse.created(storage);
            }
        }
        logger.error("Upload file failed");
        return CommonResponse.badRequest("上传文件失败");
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> delete(@RequestParam String url) throws FileNotFoundException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (principal instanceof AdminUserDetails) {
            AdminUserDetails adminUserDetails = (AdminUserDetails) principal;
            SysAdmin sysAdmin = adminUserDetails.getSysAdmin();
            Long adminId = sysAdmin.getId();
            boolean result = sysResourceService.deleteObject(url, adminId);
            if (result) {
                return CommonResponse.noContent("删除成功");
            }
        }
        return CommonResponse.badRequest("删除失败");
    }

}
